package com.tledu.email.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tledu.common.annotation.Log;
import com.tledu.common.core.controller.BaseController;
import com.tledu.common.core.domain.AjaxResult;
import com.tledu.common.enums.BusinessType;
import com.tledu.email.domain.EmailDraft;
import com.tledu.email.service.IEmailDraftService;
import com.tledu.common.utils.poi.ExcelUtil;
import com.tledu.common.core.page.TableDataInfo;

/**
 * 草稿箱Controller
 *
 * @author ruoyi
 * @date 2024-08-29
 */
@RestController
@RequestMapping("/email/drafts")
public class EmailDraftController extends BaseController {
    @Autowired
    private IEmailDraftService emailDraftService;

    // 草稿箱逻辑删除
    @PreAuthorize("@ss.hasPermi('email:drafts:edit')")
    @Log(title = "草稿箱逻辑删除实际上是更新操作", businessType = BusinessType.UPDATE)
    @PutMapping("/update/{ids}")
    public AjaxResult updateSymbol(@PathVariable Long[] ids) {
        return toAjax(emailDraftService.updateSymbol(ids));
    }

    // 根据条件筛选当前用户的草稿箱列表 selectDraftsByConditionList
    @PreAuthorize("@ss.hasPermi('email:drafts:list')")
    @GetMapping("/condition")
    public TableDataInfo selectDraftsByConditionList(EmailDraft emailDraft) {
        startPage();
        List<EmailDraft> list = emailDraftService.selectDraftsByConditionList(emailDraft);
        return getDataTable(list);
    }

    // 查询当前用户的草稿箱列表
    @PreAuthorize("@ss.hasPermi('email:drafts:list')")
    @GetMapping("/draftsList")
    public TableDataInfo selectUserDraftsList(EmailDraft emailDraft) {
        startPage();
        List<EmailDraft> list = emailDraftService.selectUserDraftsList(emailDraft);
        return getDataTable(list);
    }

    /**
     * 查询草稿箱列表
     */
    @PreAuthorize("@ss.hasPermi('email:drafts:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmailDraft emailDraft) {
        startPage();
        List<EmailDraft> list = emailDraftService.selectEmailDraftList(emailDraft);
        return getDataTable(list);
    }

    /**
     * 导出草稿箱列表
     */
    @PreAuthorize("@ss.hasPermi('email:drafts:export')")
    @Log(title = "草稿箱", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmailDraft emailDraft) {
        List<EmailDraft> list = emailDraftService.selectEmailDraftList(emailDraft);
        ExcelUtil<EmailDraft> util = new ExcelUtil<EmailDraft>(EmailDraft.class);
        util.exportExcel(response, list, "草稿箱数据");
    }

    /**
     * 获取草稿箱详细信息
     */
    @PreAuthorize("@ss.hasPermi('email:drafts:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(emailDraftService.selectEmailDraftById(id));
    }

    /**
     * 新增草稿箱
     */
    @PreAuthorize("@ss.hasPermi('email:drafts:add')")
    @Log(title = "草稿箱", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmailDraft emailDraft) {
        return toAjax(emailDraftService.insertEmailDraft(emailDraft));
    }

    /**
     * 修改草稿箱
     */
    @PreAuthorize("@ss.hasPermi('email:drafts:edit')")
    @Log(title = "草稿箱", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmailDraft emailDraft) {
        return toAjax(emailDraftService.updateEmailDraft(emailDraft));
    }

    /**
     * 删除草稿箱
     */
    @PreAuthorize("@ss.hasPermi('email:drafts:remove')")
    @Log(title = "草稿箱", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(emailDraftService.deleteEmailDraftByIds(ids));
    }
}
